package cn.est.serviceImpl;

import cn.est.constants.Constants;
import cn.est.dto.MaintainStatusEnum;
import cn.est.dto.Page;
import cn.est.mapper.MaintainOrderFlowMapper;
import cn.est.mapper.UsersMapper;
import cn.est.pojo.MaintainOrder;
import cn.est.pojo.MaintainOrderFlow;
import cn.est.pojo.Users;
import cn.est.service.MaintainOrderFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MaintainOrderFlowServiceImpl implements MaintainOrderFlowService {

    @Autowired
    private MaintainOrderFlowMapper maintainOrderFlowMapper;

    @Autowired
    private UsersMapper usersMapper;

    public MaintainOrderFlow getMaintainOrderFlowById(Long id) {
        return maintainOrderFlowMapper.getMaintainOrderFlowById(id);
    }

    public List<MaintainOrderFlow> getMaintainOrderFlowListByMap(Map<String, Object> param) {
        return maintainOrderFlowMapper.getMaintainOrderFlowListByMap(param);
    }

    public Integer getMaintainOrderFlowCountByMap(Map<String, Object> param) {
        return maintainOrderFlowMapper.getMaintainOrderFlowCountByMap(param);
    }

    public Integer qdtxAddMaintainOrderFlow(MaintainOrderFlow maintainOrderFlow) {
        maintainOrderFlow.setCreatdTime(new Date());
        return maintainOrderFlowMapper.insertMaintainOrderFlow(maintainOrderFlow);
    }

    public Integer qdtxModifyMaintainOrderFlow(MaintainOrderFlow maintainOrderFlow) {
        maintainOrderFlow.setUpdatedTime(new Date());
        return maintainOrderFlowMapper.updateMaintainOrderFlow(maintainOrderFlow);
    }

    public Integer qdtxDeleteMaintainOrderFlowById(Long id) {
        return maintainOrderFlowMapper.deleteMaintainOrderFlowById(id);
    }

    public Integer qdtxBatchDeleteMaintainOrderFlow(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return maintainOrderFlowMapper.batchDeleteMaintainOrderFlow(param);
    }

    public Page<MaintainOrderFlow> queryMaintainOrderFlowPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = maintainOrderFlowMapper.getMaintainOrderFlowCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<MaintainOrderFlow> maintainOrderFlowList = maintainOrderFlowMapper.getMaintainOrderFlowListByMap(param);
        page.setList(maintainOrderFlowList);
        return page;
    }


     /**
     * 根据订单状态保存流水状态
     * @param maintainOrder
     * @return
     */
    @Override
    public int saveByMaintainOrder(MaintainOrder maintainOrder) {
        Long userId=maintainOrder.getUserId();
        Users users = usersMapper.getUsersById(userId);
        MaintainOrderFlow maintainOrderFlow =new MaintainOrderFlow();
        maintainOrderFlow.setOperationName(users.getUserName());
        maintainOrderFlow.setOperationUId(userId);
        if(maintainOrder.getStatus()== Constants.Order.MaintainStatus.APPOINT){
            maintainOrderFlow.setOrderStatus(MaintainStatusEnum.APPONINT.getCode());
            maintainOrderFlow.setOrderStatusDesc(MaintainStatusEnum.APPONINT.getMsg());
        }else if(maintainOrder.getStatus()==Constants.Order.MaintainStatus.PAY_DEPOSIT){
            maintainOrderFlow.setOrderStatus(MaintainStatusEnum.PAY_DEPOSIT.getCode());
            maintainOrderFlow.setOrderStatusDesc(MaintainStatusEnum.PAY_DEPOSIT.getMsg());
        }
        maintainOrderFlow.setOrderNo(maintainOrder.getOrderNo());
        maintainOrderFlow.setCreatedUserId(userId);
        maintainOrderFlow.setCreatdTime(new Date());
        maintainOrderFlow.setIsDelete(Constants.EST_NOT);
     return    maintainOrderFlowMapper.insertMaintainOrderFlow(maintainOrderFlow);
    }

}
